﻿/* 
 * PerformanceDesign.cs
 * 
 * Author - Ankit Gupta and Gaurav Aggarwal
 * 
 * This file handles component design of Design tab.
 * 
 */

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Text;
using Microsoft.Win32;

partial class PerformanceControl
{
    public Label cacheSizeLabel = new Label();
    public Label cacheLocationLabel = new Label();
    public Label nonPersistentCachingLabel = new Label();
    public Label validateCacheLabel = new Label();
    public Label numCacheEntriesLabel = new Label();
    public Label connDeadTimeoutLabel = new Label();
    public Label connDeadTimeoutSecLabel = new Label();
    public Label chunkSizeLabel = new Label();
    public Label hardDeadTimeoutLabel = new Label();
    public Label hardDeadTimeoutSecLabel = new Label();
    public Label numCPULabel = new Label();
    public Label downServersLabel = new Label();
    public Label downServersSecLabel = new Label();
    public Label numDeamonsLabel = new Label();
    public Label upServersLabel = new Label();
    public Label upServersSecLabel = new Label();
    public Label numServerThreadsLabel = new Label();
    public Label callbackInvalidationLabel = new Label();
    public Label callbackInvalidationSecLabel = new Label();
    public Label MTURxInterfaceLabel = new Label();
    public Label fileLocksLabel = new Label();
    public Label fileLocksSecLabel = new Label();
    public Label tokenExpirationLabel = new Label();
    public Label tokenExpirationSecLabel = new Label();
    public Label disableRxJumbogramLabel = new Label();
    public Button cacheLocationButton = new Button();
    public Button applyButton = new Button();
    public Button cancelButton = new Button();
    public GroupBox cacheSettingsGroupBox = new GroupBox();
    public GroupBox probeIntervalsGroupBox = new GroupBox();
    public TextBox cacheSizeTextBox = new TextBox();
    public TextBox cacheLocationTextBox = new TextBox();
    public TextBox numCacheEntriesTextBox = new TextBox();
    public TextBox connDeadTimeoutTextBox = new TextBox();
    public TextBox chunkSizeTextBox = new TextBox();
    public TextBox hardDeadTimeoutTextBox = new TextBox();
    public TextBox numCPUTextBox = new TextBox();
    public TextBox downServersTextBox = new TextBox();
    public TextBox numDaemonsTextBox = new TextBox();
    public TextBox upServersTextBox = new TextBox();
    public TextBox numServerThreadsTextBox = new TextBox();
    public TextBox callbackInvalidationTextBox = new TextBox();
    public TextBox MTURxInterfaceTextBox = new TextBox();
    public TextBox fileLocksTextBox = new TextBox();
    public TextBox tokenExpirationTextBox = new TextBox();
    public CheckBox nonPersistentCachingCheckBox = new CheckBox();
    public CheckBox disableRxJumbogramCheckBox = new CheckBox();
    public ComboBox validateCacheComboBox = new ComboBox();
    public FolderBrowserDialog folderDialog = new FolderBrowserDialog();

    RegistryKey serviceParameters = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters", true);

    public PerformanceControl()
    {
        // Cache Settings
        cacheSettingsGroupBox.Location = new System.Drawing.Point(20, 10);
        cacheSettingsGroupBox.Name = "cacheSettingsGroupBox";
        cacheSettingsGroupBox.Size = new System.Drawing.Size(550, 120);
        cacheSettingsGroupBox.Text = "Cache Settings";

        probeIntervalsGroupBox.Location = new System.Drawing.Point(310, 210);
        probeIntervalsGroupBox.Name = "probeIntervalsGroupBox";
        probeIntervalsGroupBox.Size = new System.Drawing.Size(260, 175);
        probeIntervalsGroupBox.Text = "Probe intervals for...";

        // Cache Size
        cacheSizeLabel.Location = new System.Drawing.Point(30, 35);
        cacheSizeLabel.Name = "cacheSizeLabel";
        cacheSizeLabel.Size = new System.Drawing.Size(60, 30);
        cacheSizeLabel.Text = "Cache size";

        cacheSizeTextBox.Location = new System.Drawing.Point(100, 30);
        cacheSizeTextBox.Name = "cacheSizeTextBox";
        cacheSizeTextBox.Size = new System.Drawing.Size(100, 40);
        cacheSizeTextBox.Text = ((int)serviceParameters.GetValue("CacheSize", 98304)).ToString();

        // Cache Location
        cacheLocationLabel.Location = new System.Drawing.Point(250, 35);
        cacheLocationLabel.Name = "cacheLocationLabel";
        cacheLocationLabel.Size = new System.Drawing.Size(60, 30);
        cacheLocationLabel.Text = "Location";

        // Cache Path
        cacheLocationTextBox.Location = new System.Drawing.Point(320, 30);
        cacheLocationTextBox.Name = "cacheLocationTextBox";
        cacheLocationTextBox.Size = new System.Drawing.Size(150, 40);
        cacheLocationTextBox.ReadOnly = true;
        cacheLocationTextBox.Text = serviceParameters.GetValue("CachePath", "%TEMP%AFSCache").ToString();

        cacheLocationButton.Location = new System.Drawing.Point(480, 30);
        cacheLocationButton.Name = "button";
        cacheLocationButton.Size = new System.Drawing.Size(60, 23);
        cacheLocationButton.Text = "Browse...";
        cacheLocationButton.Click += new System.EventHandler(button_Click);

        // Enable/Disable non persistent caching
        nonPersistentCachingCheckBox.Location = new System.Drawing.Point(250, 70);
        nonPersistentCachingCheckBox.Name = "nonPersistentCachingCheckBox";
        nonPersistentCachingCheckBox.Size = new System.Drawing.Size(15, 20);
        nonPersistentCachingCheckBox.Checked = Convert.ToBoolean((int)serviceParameters.GetValue("NonPersistentCaching", 0));

        nonPersistentCachingLabel.Location = new System.Drawing.Point(280, 73);
        nonPersistentCachingLabel.Name = "nonPersistentCachingLabel";
        nonPersistentCachingLabel.Size = new System.Drawing.Size(200, 25);
        nonPersistentCachingLabel.Text = "Use non-persistent caching";

        // Validate cache
        validateCacheLabel.Location = new System.Drawing.Point(250, 98);
        validateCacheLabel.Name = "validateCacheLabel";
        validateCacheLabel.Text = "Validate cache";

        validateCacheComboBox.Location = new System.Drawing.Point(380, 98);
        validateCacheComboBox.Name = "validateCacheComboBox";
        validateCacheComboBox.Size = new System.Drawing.Size(170, 20);
        validateCacheComboBox.Items.Add("disabled");
        validateCacheComboBox.Items.Add("at startup");
        validateCacheComboBox.Items.Add("at shutdown");
        validateCacheComboBox.SelectedIndex = (int)serviceParameters.GetValue("ValidateCache", 0);

        // Number of Cache Entries
        numCacheEntriesLabel.Location = new System.Drawing.Point(20, 140);
        numCacheEntriesLabel.Name = "numCacheEntriesLabel";
        numCacheEntriesLabel.Size = new System.Drawing.Size(150, 30);
        numCacheEntriesLabel.Text = "Number of stat cache entries";

        numCacheEntriesTextBox.Location = new System.Drawing.Point(200, 140);
        numCacheEntriesTextBox.Name = "numCacheEntriesTextBox";
        numCacheEntriesTextBox.Size = new System.Drawing.Size(80, 40);
        numCacheEntriesTextBox.Text = ((int)serviceParameters.GetValue("Stats", 10000)).ToString();

        // Connection Dead Timeout
        connDeadTimeoutLabel.Location = new System.Drawing.Point(330, 140);
        connDeadTimeoutLabel.Name = "connDeadTimeoutLabel";
        connDeadTimeoutLabel.Size = new System.Drawing.Size(150, 30);
        connDeadTimeoutLabel.Text = "Connection Dead Timeout";

        connDeadTimeoutTextBox.Location = new System.Drawing.Point(480, 140);
        connDeadTimeoutTextBox.Name = "connDeadTimeoutTextBox";
        connDeadTimeoutTextBox.Size = new System.Drawing.Size(40, 40);
        connDeadTimeoutTextBox.Text = ((int)serviceParameters.GetValue("ConnDeadTimeout", 60)).ToString();

        connDeadTimeoutSecLabel.Location = new System.Drawing.Point(530, 140);
        connDeadTimeoutSecLabel.Name = "connDeadTimeoutSecLabel";
        connDeadTimeoutSecLabel.Size = new System.Drawing.Size(30, 30);
        connDeadTimeoutSecLabel.Text = "secs";

        // Chunk Size
        chunkSizeLabel.Location = new System.Drawing.Point(20, 180);
        chunkSizeLabel.Name = "chunkSizeLabel";
        chunkSizeLabel.Size = new System.Drawing.Size(150, 30);
        chunkSizeLabel.Text = "Chunk size";

        chunkSizeTextBox.Location = new System.Drawing.Point(200, 180);
        chunkSizeTextBox.Name = "chunkSizeTextBox";
        chunkSizeTextBox.Size = new System.Drawing.Size(80, 40);
        chunkSizeTextBox.Text = ((int)serviceParameters.GetValue("ChunkSize", 20)).ToString();

        // Hard Dead Time out
        hardDeadTimeoutLabel.Location = new System.Drawing.Point(330, 180);
        hardDeadTimeoutLabel.Name = "hardDeadTimeoutLabel";
        hardDeadTimeoutLabel.Size = new System.Drawing.Size(150, 30);
        hardDeadTimeoutLabel.Text = "Hard Dead Timeout";

        hardDeadTimeoutTextBox.Location = new System.Drawing.Point(480, 180);
        hardDeadTimeoutTextBox.Name = "hardDeadTimeoutTextBox";
        hardDeadTimeoutTextBox.Size = new System.Drawing.Size(40, 40);
        hardDeadTimeoutTextBox.Text = ((int)serviceParameters.GetValue("HardDeadTimeout", 120)).ToString();

        hardDeadTimeoutSecLabel.Location = new System.Drawing.Point(530, 180);
        hardDeadTimeoutSecLabel.Name = "hardDeadTimeoutSecLabel";
        hardDeadTimeoutSecLabel.Size = new System.Drawing.Size(30, 30);
        hardDeadTimeoutSecLabel.Text = "secs";

        // Maximum Number of CPUs to be used
        numCPULabel.Location = new System.Drawing.Point(20, 220);
        numCPULabel.Name = "numCPULabel";
        numCPULabel.Size = new System.Drawing.Size(170, 30);
        numCPULabel.Text = "Maximum no. of CPUs to use";

        numCPUTextBox.Location = new System.Drawing.Point(200, 220);
        numCPUTextBox.Name = "numCPUTextBox";
        numCPUTextBox.Size = new System.Drawing.Size(80, 40);
        numCPUTextBox.Text = (serviceParameters.GetValue("MaxCPUs", "(Unlimited)")).ToString();
        
        // Number of down Servers
        downServersLabel.Location = new System.Drawing.Point(330, 230);
        downServersLabel.Name = "downServersLabel";
        downServersLabel.Size = new System.Drawing.Size(150, 30);
        downServersLabel.Text = "\"Down\" servers";

        downServersTextBox.Location = new System.Drawing.Point(480, 230);
        downServersTextBox.Name = "downServersTextBox";
        downServersTextBox.Size = new System.Drawing.Size(40, 40);
        downServersTextBox.Text = ((int)serviceParameters.GetValue("daemonCheckDownInterval", 180)).ToString();

        downServersSecLabel.Location = new System.Drawing.Point(530, 230);
        downServersSecLabel.Name = "downServersSecLabel";
        downServersSecLabel.Size = new System.Drawing.Size(30, 30);
        downServersSecLabel.Text = "secs";

        // Number of backgrond daemons
        numDeamonsLabel.Location = new System.Drawing.Point(20, 260);
        numDeamonsLabel.Name = "numDeamonsLabel";
        numDeamonsLabel.Size = new System.Drawing.Size(170, 30);
        numDeamonsLabel.Text = "Number of background daemons";

        numDaemonsTextBox.Location = new System.Drawing.Point(200, 260);
        numDaemonsTextBox.Name = "numDaemonsTextBox";
        numDaemonsTextBox.Size = new System.Drawing.Size(80, 40);
        numDaemonsTextBox.Text = ((int)serviceParameters.GetValue("Daemons", 2)).ToString();

        // Up servers
        upServersLabel.Location = new System.Drawing.Point(330, 260);
        upServersLabel.Name = "upServersLabel";
        upServersLabel.Size = new System.Drawing.Size(150, 30);
        upServersLabel.Text = "\"Up\" servers";

        upServersTextBox.Location = new System.Drawing.Point(480, 260);
        upServersTextBox.Name = "upServersTextBox";
        upServersTextBox.Size = new System.Drawing.Size(40, 40);
        upServersTextBox.Text = ((int)serviceParameters.GetValue("daemonCheckUpInterval", 600)).ToString();

        upServersSecLabel.Location = new System.Drawing.Point(530, 260);
        upServersSecLabel.Name = "upServersSecLabel";
        upServersSecLabel.Size = new System.Drawing.Size(30, 30);
        upServersSecLabel.Text = "secs";

        // Number of Server Threads
        numServerThreadsLabel.Location = new System.Drawing.Point(20, 300);
        numServerThreadsLabel.Name = "numServerThreadsLabel";
        numServerThreadsLabel.Size = new System.Drawing.Size(150, 30);
        numServerThreadsLabel.Text = "Number of server threads";

        numServerThreadsTextBox.Location = new System.Drawing.Point(200, 300);
        numServerThreadsTextBox.Name = "numServerThreadsTextBox";
        numServerThreadsTextBox.Size = new System.Drawing.Size(80, 40);
        numServerThreadsTextBox.Text = ((int)serviceParameters.GetValue("ServerThreads", 25)).ToString();

        // Callback invalidation
        callbackInvalidationLabel.Location = new System.Drawing.Point(330, 290);
        callbackInvalidationLabel.Name = "callbackInvalidationLabel";
        callbackInvalidationLabel.Size = new System.Drawing.Size(150, 30);
        callbackInvalidationLabel.Text = "Callback invalidation";

        callbackInvalidationTextBox.Location = new System.Drawing.Point(480, 290);
        callbackInvalidationTextBox.Name = "callbackInvalidationTextBox";
        callbackInvalidationTextBox.Size = new System.Drawing.Size(40, 40);
        callbackInvalidationTextBox.Text = ((int)serviceParameters.GetValue("daemonCheckCBInterval", 60)).ToString();

        callbackInvalidationSecLabel.Location = new System.Drawing.Point(530, 290);
        callbackInvalidationSecLabel.Name = "callbackInvalidationSecLabel";
        callbackInvalidationSecLabel.Size = new System.Drawing.Size(30, 30);
        callbackInvalidationSecLabel.Text = "secs";

        // MTU for the RX interface
        MTURxInterfaceLabel.Location = new System.Drawing.Point(20, 340);
        MTURxInterfaceLabel.Name = "MTURxInterfaceLabel";
        MTURxInterfaceLabel.Size = new System.Drawing.Size(150, 30);
        MTURxInterfaceLabel.Text = "MTU for the RX interface";

        MTURxInterfaceTextBox.Location = new System.Drawing.Point(200, 340);
        MTURxInterfaceTextBox.Name = "MTURxInterfaceTextBox";
        MTURxInterfaceTextBox.Size = new System.Drawing.Size(80, 40);
        MTURxInterfaceTextBox.Text = (serviceParameters.GetValue("RxMaxMTU", "(Automatic)")).ToString();

        // File locks
        fileLocksLabel.Location = new System.Drawing.Point(330, 320);
        fileLocksLabel.Name = "fileLocksLabel";
        fileLocksLabel.Size = new System.Drawing.Size(150, 30);
        fileLocksLabel.Text = "File locks";

        fileLocksTextBox.Location = new System.Drawing.Point(480, 320);
        fileLocksTextBox.Name = "fileLocksTextBox";
        fileLocksTextBox.Size = new System.Drawing.Size(40, 40);
        fileLocksTextBox.Text = ((int)serviceParameters.GetValue("daemonCheckLockInterval", 60)).ToString();

        fileLocksSecLabel.Location = new System.Drawing.Point(530, 320);
        fileLocksSecLabel.Name = "fileLocksSecLabel";
        fileLocksSecLabel.Size = new System.Drawing.Size(30, 30);
        fileLocksSecLabel.Text = "secs";

        // Token Expiration
        tokenExpirationLabel.Location = new System.Drawing.Point(330, 350);
        tokenExpirationLabel.Name = "tokenExpirationLabel";
        tokenExpirationLabel.Size = new System.Drawing.Size(150, 30);
        tokenExpirationLabel.Text = "Token Expiration";

        tokenExpirationTextBox.Location = new System.Drawing.Point(480, 350);
        tokenExpirationTextBox.Name = "tokenExpirationTextBox";
        tokenExpirationTextBox.Size = new System.Drawing.Size(40, 40);
        tokenExpirationTextBox.Text = ((int)serviceParameters.GetValue("daemonCheckTokenInterval", 180)).ToString();

        tokenExpirationSecLabel.Location = new System.Drawing.Point(530, 350);
        tokenExpirationSecLabel.Name = "tokenExpirationSecLabel";
        tokenExpirationSecLabel.Size = new System.Drawing.Size(30, 30);
        tokenExpirationSecLabel.Text = "secs";

        // Enable/Disable use of RX jumbograms
        disableRxJumbogramCheckBox.Location = new System.Drawing.Point(20, 377);
        disableRxJumbogramCheckBox.Name = "disableRxJumbogramCheckBox";
        disableRxJumbogramCheckBox.Size = new System.Drawing.Size(15, 20);
        disableRxJumbogramCheckBox.Checked = Convert.ToBoolean((int)serviceParameters.GetValue("RxNoJumbo", 0));

        disableRxJumbogramLabel.Location = new System.Drawing.Point(40, 380);
        disableRxJumbogramLabel.Name = "disableRxJumbogramLabel";
        disableRxJumbogramLabel.Size = new System.Drawing.Size(200, 25);
        disableRxJumbogramLabel.Text = "Disable use of RX jumbograms";

        // Apply button
        applyButton.Location = new System.Drawing.Point(20, 460);
        applyButton.Name = "applyButton";
        applyButton.Size = new System.Drawing.Size(100, 23);
        applyButton.Text = "Apply";
        applyButton.Click += new System.EventHandler(applyButton_Click);

        // Cancel button
        cancelButton.Location = new System.Drawing.Point(140, 460);
        cancelButton.Name = "cancelButton";
        cancelButton.Size = new System.Drawing.Size(100, 23);
        cancelButton.Text = "Cancel";
        cancelButton.Click += new System.EventHandler(cancelButton_Click);
    }

    private void button_Click(object sender, EventArgs e)
    {
        DialogResult result = folderDialog.ShowDialog();
        if (result == DialogResult.OK)
        {
            string file = folderDialog.SelectedPath;
            cacheLocationTextBox.Text = file;
        }
    }
}